package com.cskaoyan.order.service.impl;

import com.cskaoyan.mall.commons.result.ServiceCallResponse;
import com.cskaoyan.mall.commons.result.ServiceCallResponseUtil;
import com.cskaoyan.order.dal.entitys.Order;
import com.cskaoyan.order.dal.entitys.OrderItem;
import com.cskaoyan.order.dal.entitys.Stock;
import com.cskaoyan.order.dal.persistence.OrderItemMapper;
import com.cskaoyan.order.dal.persistence.OrderMapper;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.service.PayOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @ClassName:PayOrderServiceImpl
 * @Auther: HeXianZu
 * @Description:
 * @Date: 2022/10/31 20:16
 */
@Service
public class PayOrderServiceImpl implements PayOrderService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    StockMapper stockMapper;

    @Override
    public ServiceCallResponse payOrderSuccess(String orderId) {
        // 修改订单状态
        Order order = new Order();
        order.setStatus(1);
        order.setOrderId(orderId);
        int i = orderMapper.updateByPrimaryKeySelective(order);
        // 修改订单商品对应lock_count
        Example example = new Example(OrderItem.class);
        example.createCriteria().andEqualTo("orderId", orderId);
        List<OrderItem> orderItems = orderItemMapper.selectByExample(example);
        if (orderItems != null) {
            for (OrderItem orderItem : orderItems) {
                Stock stock = new Stock();
                stock.setItemId(orderItem.getItemId());
                stock.setLockCount(-orderItem.getNum());
                stock.setStockCount(0L);
                stockMapper.updateStock(stock);
            }
        }
        return new ServiceCallResponseUtil<>().ok(null);
    }
}
